package com.cars.bim.generator.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cars.bim.generator.entity.TbDakaEntity;
import com.cars.bim.generator.service.TbDakaService;
import com.cars.framework.common.utils.DateUtils;
import com.cars.framework.common.utils.PageUtils;
import com.cars.framework.common.utils.Query;
import com.cars.framework.common.utils.R;
import com.cars.framework.modules.sys.controller.AbstractController;
import com.cars.framework.modules.sys.entity.SysDeptEntity;
import com.cars.framework.modules.sys.entity.SysUserEntity;
import com.cars.framework.modules.sys.service.SysDeptService;




/**
 * 
 * 
 * @author admin
 * @email admin@gmail.com
 * @date 2018-05-13 15:41:53
 */
@RestController
@RequestMapping("/generator/tbdaka")
public class TbDakaController  extends AbstractController{
	@Autowired
	private TbDakaService tbDakaService;
	
	@Autowired
	private SysDeptService sysDeptService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:tbdaka:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		  Query query = null;
	        SysUserEntity  user =   getUser();
	        if(!"admin".equals(user.getUsername())){
	        	
	        	params.put("name",user.getUsername());
	        }
	        query =  new Query(params);

		List<TbDakaEntity> tbDakaList = tbDakaService.queryList(query);
		int total = tbDakaService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(tbDakaList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	
	
	
	
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:tbdaka:info")
	public R info(@PathVariable("id") Integer id){
		TbDakaEntity tbDaka = tbDakaService.queryObject(id);
		
		return R.ok().put("tbDaka", tbDaka);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("generator:tbdaka:save")
	public R save(@RequestBody TbDakaEntity tbDaka){
		tbDakaService.save(tbDaka);
		
		return R.ok();
	}
	
	/**
	 * 上班打卡
	 */
	@RequestMapping("/shangban")
	public R shangban(){
		
		String date = DateUtils.formatDate(new Date());
	    SysUserEntity  user =   getUser();
	    String userName = user.getUsername();
	    TbDakaEntity tbDaka = new TbDakaEntity();
	    String[] dateArr = date.split(" ");
	    String beginDate = dateArr[1];//上班打卡时间
	    
	    String[] spiltDate = dateArr[0].split("-");
	    tbDaka.setSYear(spiltDate[0]);
	    tbDaka.setSMonth(spiltDate[1]);
	    tbDaka.setSDate(spiltDate[2]);
	    tbDaka.setBeginTime(beginDate);
	    tbDaka.setName(userName);
	    SysDeptEntity dep = sysDeptService.queryObject(user.getDeptId());
	    tbDaka.setEmp(dep.getName());
	    tbDaka.setCreateTime(date);
	    
	    TbDakaEntity entity =   tbDakaService.queryObjectByDaka(tbDaka);
	    if(entity!=null){
	    	entity.setBeginTime(beginDate);
	    	tbDakaService.update(entity);
	    }else
	    {
	    	 tbDakaService.save(tbDaka);
	    }
		
		return R.ok();
	}
	/**
	 * 下班打卡
	 */
	@RequestMapping("/xiaban")
	public R xiaban(){
		
		String date = DateUtils.formatDate(new Date());
	    SysUserEntity  user =   getUser();
	    String userName = user.getUsername();
	    TbDakaEntity tbDaka = new TbDakaEntity();
	    String[] dateArr = date.split(" ");
	    String beginDate = dateArr[1];//上班打卡时间
	    
	    String[] spiltDate = dateArr[0].split("-");
	    tbDaka.setSYear(spiltDate[0]);
	    tbDaka.setSMonth(spiltDate[1]);
	    tbDaka.setSDate(spiltDate[2]);
	    tbDaka.setEndTime(beginDate);
	    tbDaka.setName(userName);
	    SysDeptEntity dep = sysDeptService.queryObject(user.getDeptId());
	    tbDaka.setEmp(dep.getName());
	    tbDaka.setCreateTime(date);
	    
	    TbDakaEntity entity =   tbDakaService.queryObjectByDaka(tbDaka);
	    if(entity!=null){
	    	entity.setEndTime(beginDate);
	    	tbDakaService.update(entity);
	    }else
	    {
	    	 tbDakaService.save(tbDaka);
	    }
		
		return R.ok();
	}
	
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("generator:tbdaka:update")
	public R update(@RequestBody TbDakaEntity tbDaka){
		tbDakaService.update(tbDaka);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:tbdaka:delete")
	public R delete(@RequestBody Integer[] ids){
		tbDakaService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
